Subject: Re: `char *a="text"` same as `char a[] = "text"` ??
From: [email-masked] (John William Chambless)
Date: 4 Apr 1994 10:52:33 -0500
Newsgroups: comp.lang.c

In article <[email-masked]>, Joe Dunn <[email-masked]> wrote:
>In article <[email-masked]>, [email-masked] (Hendrik Jan Veenstra) writes:
>|> First of all: there have already been several correct follow-ups to the
>|> original question. If you're *not* sure about the correctness of your reply,
>|> you might consider not posting it...

>you should follow your own advice...

ROTFL!
The irony of your reply is classic considering how totally incorrect
your post is.

>this is absolutely wrong!! you can modify a exactly like this. only in c++
>if you declare the pointer to a string a constant can you not modify the string

Oh, really? Wow, you should immediately notify Dennis Ritchie, Brian Kernighan,
and the ANSI comittee on C! They ALL have it wrong!

>a literal string is nothing more than an array. try it:
                                                ^^^^^^^^
Oh, okay. Since it works on YOUR copy of the FooBar WhizBang
C compiler on _your_ machine, it MUST be valid code!

>        char *a = "text";
>        printf("a = %s\n",a);
>        a[1] = 'f';
>        printf("a = %s\n",a);

*deep sigh*
One more time, class--from the top:

From the ANSI/ISO C Standard, 6.5.7:

	"...the declaration

	char *p = "abc";

	defines p with type "pointer to char" that is initialized to
	point to an object with type "array of char" with length 4 whose 
	elements are initialized with a character string literal. If an
	attempt is made to use p to modify the contents of the array,
	the behaviour is undefined."

The fourth paragraph of section 5.5 of K&R2 says the same thing.

If you're unsure what "undefined" means, it's in Annex G of the standard,
or you can read all about it in the comp.lang.c archives.
>
>sorry, no difference. in a real operating system that does not have the
>segmented memory of DOS, code space and data space, these definitions can
>be used interchangaebly. 

The OS is irrelevant.
The fact is  that some compilers will accept bletcherous code such as
your example, but the Standard does not require them to.

Run lint on your example, and see what it says.

> Naturally, when DOS grows up and becomes a true 32 bit operating system
> then we can all go back to using the statements interchangeably.

Why use them interchangably?
Most of us have already solved the problem  by learning to write
ANSI-compliant code. Code that depends on the quirks of a particular
compiler is dangerous.
>
> 

-- 
*  Billy Chambless                  University of Southern Mississippi
* "IBM is not a necessary evil.  IBM is not necessary."  -- Ted Nelson